mui.init();
mui.plusReady(function(){
	var sessionId=(plus.navigator.getCookie("sessionId")==null)?"":plus.navigator.getCookie("sessionId");
	var stockname = unescape(GetQueryString("stockname"));
	var clientname = unescape(GetQueryString("clientname"));
	var billnumber = GetQueryString("billnumber");
	var maxnumber = GetQueryString("maxnumber");
	var c_id = GetQueryString("c_id");
	var stockno = GetQueryString("stockno");
	document.getElementById("stockno").value = stockno;
	document.getElementById("stockname").value = stockname;
	document.getElementById("clientname").value = clientname;
	document.getElementById("billnumber").value = billnumber;
	document.getElementById("maxnumber").value = maxnumber;
	document.getElementById("c_id").value = c_id;
	var span = "<span style='color:sandybrown'><剩余吨数：" + maxnumber + "吨></span>";
	$("#maxnumber1").append(span);
	//获取当前用户的司机
	mui.getJSON("json/locat.json", function(data) {
		var locat = data["url"];
			var factoryid=data["factoryid"];
			var factoryId=(plus.navigator.getCookie("factoryid")==null)?"":plus.navigator.getCookie("factoryid");
					if(factoryId != factoryid)
					window.location.href="login.html";
		mui.ajax(locat + "/clientUser/driver_list?sessionId="+sessionId, {
			type: "GET",
			success: function(data, status, xhr) {
				if("error" == data["msg"]) {
					window.location.href = "login.html";
				} else {
					//司机
					if("drierror" == data["msg"]) {
						swal("没有司机不能提货 ", "先去添加司机", "error");
						swal({
							title: "您还没有提货司机",
							text: "是否添加提货司机",
							type: "warning",
							showCancelButton: true,
							closeOnConfirm: false,
							cancelButtonText: "取消",
							confirmButtonText: "确定",
							confirmButtonColor: "#ec6c62"
						}, function() {
							window.location.href = "tjsja.html";
						})

					} else {
						for(i = data["list_driver"].length - 1; i >= 0; i--) {
							document.getElementById("driverid").options.add(new Option(data["list_driver"][i]["name"], data["list_driver"][i]["d_id"]));
						}
					}
				}

			},
			error: function(data) {
				plus.ui.toast("", "服务异常！", "error");
				window.location.href = login.html;
			}
		});
	});

});

//保存订单
function save() {

	var billnumber = $("#billnumber").val();
	var c_id = $("#c_id").val();
	var stockno = $("#stockno").val();
	var stockname = $("#stockname").val();
	var driverid = $("#driverid").val();
	var goodsnumber = $("#goodsnumber").val();
	var thrq = $("#thrq").val();
	var thrqmsg = $("#thrqmsg").val();
	var numbermsg = $("#numbermsg").val();
	var sessionId=(plus.navigator.getCookie("sessionId")==null)?"":plus.navigator.getCookie("sessionId");
	if(billnumber == "" || c_id == "" || stockname == "" || driverid == "" || goodsnumber == "" || thrq == "") {

		$("#doc-vld-ajax").submit();
		return false;
	} else {
		if(thrqmsg == "error" || numbermsg == "error") {
			swal("填写有误，请认真填写", "", "error");
		} else {
			var id = "body-loading";
			$(window).loading({
				action: "open",
				id: id,
				font: 40
			});
			mui.getJSON("json/locat.json", function(data) {
				var locat = data["url"];
				var factoryid = data["factoryid"];
				mui.ajax(locat + "/shop/place_order", {
					type: "POST",
					data: {
						billnumber: billnumber,
						c_id: c_id,
						stockno: stockno,
						stockname: stockname,
						driverid: driverid,
						goodsnumber: goodsnumber,
						thrq: thrq,
						factoryid: factoryid,
						sessionId:sessionId
					},
					success: function(data, status, xhr) {
						var time = setTimeout(function() {
							//关闭
							$(window).loading({
								action: "close",
								id: id
							});
						}, 100);
						swal("下单成功！");
						window.location.href = "lsdd.html";
					},
					error: function(data) {
						var time = setTimeout(function() {
							//关闭
							$(window).loading({
								action: "close",
								id: id
							});
						}, 100);
						plus.ui.toast("服务异常", "请稍后再试", "error");
						window.location.href = "login.html";

					}
				});
			});

		}
	}
	//提货单保存

}

function chkvalue(txt) {

	var maxgoodnumber = Number(GetQueryString("maxnumber"));
	//swal(maxgoodnumber+"sasasasa"+txt.value+"ssssss");
	var goodsnumber = Number(txt.value);

	if(maxgoodnumber < goodsnumber) {
		swal("提货吨数不能大于剩余货物吨数", "", "error");
		$("#goodsnumber").css({
			"color": "red",
		});
		document.getElementById("numbermsg").value = "error";
	} else {
		if(txt.value <= 0) {
			swal("提货吨数不能小于或等于0", "", "error");
			$("#goodsnumber").css({
				"color": "red",
			});
			document.getElementById("numbermsg").value = "error";
		} else {
			$("#goodsnumber").css({
				"color": "black",
			});
			document.getElementById("numbermsg").value = "success";
		}

	}
}

function chkDate() {
	var pdate = document.getElementById('thrq');
	var d = new Date;
	var today = new Date(d.getFullYear(), d.getMonth(), d.getDate());

	var reg = /\d+/g;
	var temp = pdate.value.match(reg);

	var foday = new Date(temp[0], parseInt(temp[1]) - 1, temp[2]);

	if(foday < today) {
		swal("提货日期不能在今天之前", " ", "error");
		$("#thrq").css({
			"color": "red",
		});
		document.getElementById("thrqmsg").value = "error";

	} else {
		$("#thrq").css({
			"color": "black",
		});
		document.getElementById("thrqmsg").value = "success";
	}

}

function goback() {

	window.location.href='cwddlb.html';

}

function GetQueryString(name) {
	var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
	var r = window.location.search.substr(1).match(reg);
	if(r != null) return unescape(r[2]);
	return null;
}